package com.mvvmhabit.org.binding.command

class BindingCommand<T> {

    private var execute: BindingAction? = null
    private var consumer: BindingConsumer<T>? = null
    private var canExecute: BindingFunction<Boolean>? = null

    constructor(execute: BindingAction) {
        this.execute = execute
    }

    constructor(execute: BindingConsumer<T>) {
        this.consumer = execute
    }

    constructor(execute: BindingAction, canExecute: BindingFunction<Boolean>) {
        this.execute = execute
        this.canExecute = canExecute
    }

    constructor(execute: BindingConsumer<T>, canExecute: BindingFunction<Boolean>) {
        this.consumer = execute
        this.canExecute = canExecute
    }

    fun execute() {
        if (execute != null && canExecute()) {
            execute!!.call()
        }
    }

    fun execute(parameter: T) {
        if (consumer != null && canExecute()) {
            consumer!!.call(parameter)
        }
    }

    private fun canExecute(): Boolean {
        if (canExecute == null) {
            return true
        }
        return canExecute!!.call()
    }
}